/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dubbo.config.spring;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.ModuleConfig;
import org.apache.dubbo.config.MonitorConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;

import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.addApplicationListener;

/**
 * ServiceFactoryBean
 *
 * @export
 */
public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean, DisposableBean,
    ApplicationContextAware, ApplicationListener<ContextRefreshedEvent>, BeanNameAware,
    ApplicationEventPublisherAware {


  private static final long serialVersionUID = 213195494150089726L;

  private final transient Service service;

  private transient ApplicationContext applicationContext;

  private transient String beanName;

  private transient boolean supportedApplicationListener;

  private ApplicationEventPublisher applicationEventPublisher;

  public ServiceBean() {
    super();
    this.service = null;
  }

  public ServiceBean(Service service) {
    super(service);
    this.service = service;
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
    SpringExtensionFactory.addApplicationContext(applicationContext);
    supportedApplicationListener = addApplicationListener(applicationContext, this);
  }

  @Override
  public void setBeanName(String name) {
    this.beanName = name;
  }

  /**
   * Gets associated {@link Service}
   *
   * @return associated {@link Service}
   */
  public Service getService() {
    return service;
  }

  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {
    if (!isExported() && !isUnexported()) {
      if (logger.isInfoEnabled()) {
        logger.info("The service ready on spring started. service: " + getInterface());
      }
      export();
    }
  }

  @Override
  @SuppressWarnings({"unchecked", "deprecation"})
  public void afterPropertiesSet() throws Exception {
    if (getProvider() == null) {
      Map<String, ProviderConfig> providerConfigMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class, false,
                  false);
      if (providerConfigMap != null && providerConfigMap.size() > 0) {
        Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null
            : BeanFactoryUtils
                .beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false,
                    false);
        if (CollectionUtils.isEmptyMap(protocolConfigMap)
            && providerConfigMap.size() > 1) { // backward compatibility
          List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
          for (ProviderConfig config : providerConfigMap.values()) {
            if (config.isDefault() != null && config.isDefault()) {
              providerConfigs.add(config);
            }
          }
          if (!providerConfigs.isEmpty()) {
            setProviders(providerConfigs);
          }
        } else {
          ProviderConfig providerConfig = null;
          for (ProviderConfig config : providerConfigMap.values()) {
            if (config.isDefault() == null || config.isDefault()) {
              if (providerConfig != null) {
                throw new IllegalStateException(
                    "Duplicate provider configs: " + providerConfig + " and " + config);
              }
              providerConfig = config;
            }
          }
          if (providerConfig != null) {
            setProvider(providerConfig);
          }
        }
      }
    }
    if (getApplication() == null
        && (getProvider() == null || getProvider().getApplication() == null)) {
      Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false,
                  false);
      if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
        ApplicationConfig applicationConfig = null;
        for (ApplicationConfig config : applicationConfigMap.values()) {
          if (applicationConfig != null) {
            throw new IllegalStateException(
                "Duplicate application configs: " + applicationConfig + " and " + config);
          }
          applicationConfig = config;
        }
        if (applicationConfig != null) {
          setApplication(applicationConfig);
        }
      }
    }
    if (getModule() == null
        && (getProvider() == null || getProvider().getModule() == null)) {
      Map<String, ModuleConfig> moduleConfigMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, ModuleConfig.class, false, false);
      if (moduleConfigMap != null && moduleConfigMap.size() > 0) {
        ModuleConfig moduleConfig = null;
        for (ModuleConfig config : moduleConfigMap.values()) {
          if (config.isDefault() == null || config.isDefault()) {
            if (moduleConfig != null) {
              throw new IllegalStateException(
                  "Duplicate module configs: " + moduleConfig + " and " + config);
            }
            moduleConfig = config;
          }
        }
        if (moduleConfig != null) {
          setModule(moduleConfig);
        }
      }
    }

    if (StringUtils.isEmpty(getRegistryIds())) {
      if (getApplication() != null && StringUtils.isNotEmpty(getApplication().getRegistryIds())) {
        setRegistryIds(getApplication().getRegistryIds());
      }
      if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getRegistryIds())) {
        setRegistryIds(getProvider().getRegistryIds());
      }
    }

    if ((CollectionUtils.isEmpty(getRegistries()))
        && (getProvider() == null || CollectionUtils.isEmpty(getProvider().getRegistries()))
        && (getApplication() == null || CollectionUtils.isEmpty(getApplication().getRegistries()))) {
      Map<String, RegistryConfig> registryConfigMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class, false, false);
      if (CollectionUtils.isNotEmptyMap(registryConfigMap)) {
        List<RegistryConfig> registryConfigs = new ArrayList<>();
        if (StringUtils.isNotEmpty(registryIds)) {
          Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(registryIds)).forEach(id -> {
            if (registryConfigMap.containsKey(id)) {
              registryConfigs.add(registryConfigMap.get(id));
            }
          });
        }

        if (registryConfigs.isEmpty()) {
          for (RegistryConfig config : registryConfigMap.values()) {
            if (StringUtils.isEmpty(registryIds)) {
              registryConfigs.add(config);
            }
          }
        }
        if (!registryConfigs.isEmpty()) {
          super.setRegistries(registryConfigs);
        }
      }
    }
    if (getMetadataReportConfig() == null) {
      Map<String, MetadataReportConfig> metadataReportConfigMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, MetadataReportConfig.class, false, false);
      if (metadataReportConfigMap != null && metadataReportConfigMap.size() == 1) {
        super.setMetadataReportConfig(metadataReportConfigMap.values().iterator().next());
      } else if (metadataReportConfigMap != null && metadataReportConfigMap.size() > 1) {
        throw new IllegalStateException(
            "Multiple MetadataReport configs: " + metadataReportConfigMap);
      }
    }

    if (getConfigCenter() == null) {
      Map<String, ConfigCenterConfig> configenterMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, ConfigCenterConfig.class, false, false);
      if (configenterMap != null && configenterMap.size() == 1) {
        super.setConfigCenter(configenterMap.values().iterator().next());
      } else if (configenterMap != null && configenterMap.size() > 1) {
        throw new IllegalStateException("Multiple ConfigCenter found:" + configenterMap);
      }
    }

    if (getMonitor() == null
        && (getProvider() == null || getProvider().getMonitor() == null)
        && (getApplication() == null || getApplication().getMonitor() == null)) {
      Map<String, MonitorConfig> monitorConfigMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, MonitorConfig.class, false, false);
      if (monitorConfigMap != null && monitorConfigMap.size() > 0) {
        MonitorConfig monitorConfig = null;
        for (MonitorConfig config : monitorConfigMap.values()) {
          if (config.isDefault() == null || config.isDefault()) {
            if (monitorConfig != null) {
              throw new IllegalStateException(
                  "Duplicate monitor configs: " + monitorConfig + " and " + config);
            }
            monitorConfig = config;
          }
        }
        if (monitorConfig != null) {
          setMonitor(monitorConfig);
        }
      }
    }

    if (StringUtils.isEmpty(getProtocolIds())) {
      if (getProvider() != null && StringUtils.isNotEmpty(getProvider().getProtocolIds())) {
        setProtocolIds(getProvider().getProtocolIds());
      }
    }

    if (CollectionUtils.isEmpty(getProtocols())
        && (getProvider() == null || CollectionUtils.isEmpty(getProvider().getProtocols()))) {
      Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null
          : BeanFactoryUtils
              .beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false);
      if (protocolConfigMap != null && protocolConfigMap.size() > 0) {
        List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
        if (StringUtils.isNotEmpty(getProtocolIds())) {
          Arrays.stream(Constants.COMMA_SPLIT_PATTERN.split(getProtocolIds()))
              .forEach(id -> {
                if (protocolConfigMap.containsKey(id)) {
                  protocolConfigs.add(protocolConfigMap.get(id));
                }
              });
        }

        if (protocolConfigs.isEmpty()) {
          for (ProtocolConfig config : protocolConfigMap.values()) {
            if (StringUtils.isEmpty(protocolIds)) {
              protocolConfigs.add(config);
            }
          }
        }

        if (!protocolConfigs.isEmpty()) {
          super.setProtocols(protocolConfigs);
        }
      }
    }
    if (StringUtils.isEmpty(getPath())) {
      if (StringUtils.isNotEmpty(beanName)
          && StringUtils.isNotEmpty(getInterface())
          && beanName.startsWith(getInterface())) {
        setPath(beanName);
      }
    }
    if (!supportedApplicationListener) {
      export();
    }
  }

  /**
   * Get the name of {@link ServiceBean}
   *
   * @return {@link ServiceBean}'s name
   * @since 2.6.5
   */
  public String getBeanName() {
    return this.beanName;
  }

  /**
   * @since 2.6.5
   */
  @Override
  public void export() {
    super.export();
    // Publish ServiceBeanExportedEvent
    publishExportEvent();
  }

  /**
   * @since 2.6.5
   */
  private void publishExportEvent() {
    ServiceBeanExportedEvent exportEvent = new ServiceBeanExportedEvent(this);
    applicationEventPublisher.publishEvent(exportEvent);
  }

  @Override
  public void destroy() throws Exception {
    // no need to call unexport() here, see
    // org.apache.dubbo.config.spring.extension.SpringExtensionFactory.ShutdownHookListener
  }

  // merged from dubbox
  @Override
  protected Class getServiceClass(T ref) {
    if (AopUtils.isAopProxy(ref)) {
      return AopUtils.getTargetClass(ref);
    }
    return super.getServiceClass(ref);
  }

  /**
   * @since 2.6.5
   */
  @Override
  public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
    this.applicationEventPublisher = applicationEventPublisher;
  }
}